// skin_button.c
#include "skin.h"

int skin_button_onmousemove(void* self, SDL_Event* e)
{
	PSKIN_BUTTON p = (PSKIN_BUTTON) self;
	if (p)
	{		
		PSKIN_CLIENT control = (PSKIN_CLIENT) p;

		if (ptinrect(e->motion.x, e->motion.y, &control->rect))
		{
			skin_set_activecontrol((PSKIN_WINDOW)control->parent, control);
			if (p->button_state & BUTTON_HOVER == BUTTON_HOVER)
			{

			}
			else
			{
				// change client_surface
				if (p->hover)
				{

				}
				else
				{
					SDL_Rect rect;
					// set default color
					rect.x = rect.y = 0;
					rect.w = control->rect.cx - control->rect.x;
					rect.h = control->rect.cy - control->rect.y;

					SDL_FillRect(control->client_surface, &rect, SDL_MapRGB(control->client_surface->format, 255, 255, 255));
				}
				skin_windowupdate((PSKIN_WINDOW) control->parent);
				p->button_state |= BUTTON_HOVER;
			}
		}
		else
		{
			if ((p->button_state & BUTTON_CLICKED) == BUTTON_CLICKED)
			{
				return 0;
			}
			// active control reset
			p->button_state = p->button_state & ~(BUTTON_HOVER);
			if (p->normal)
			{

			}
			else
			{
				SDL_Rect rect;
				// set default color
				rect.x = rect.y = 0;
				rect.w = control->rect.cx - control->rect.x;
				rect.h = control->rect.cy - control->rect.y;

				SDL_FillRect(control->client_surface, &rect, SDL_MapRGB(control->client_surface->format, 128, 128, 128));
			}
			skin_set_activecontrol((PSKIN_WINDOW)control->parent, 0);
			skin_windowupdate((PSKIN_WINDOW) control->parent);
		}
				
	}
	return 0;
}

int skin_button_onmousebuttondown(void* self, SDL_Event* e)
{
	PSKIN_BUTTON p = (PSKIN_BUTTON) self;
	if (p)
	{		
		PSKIN_CLIENT control = (PSKIN_CLIENT) p;

		if (ptinrect(e->motion.x, e->motion.y, &control->rect))
		{
			p->button_state = p->button_state | BUTTON_CLICKED;
			if (p->clicked)
			{

			}
			else
			{
				SDL_Rect rect;
				// set default color
				rect.x = rect.y = 0;
				rect.w = control->rect.cx - control->rect.x;
				rect.h = control->rect.cy - control->rect.y;

				SDL_FillRect(control->client_surface, &rect, SDL_MapRGB(control->client_surface->format, 200, 200, 200));
			}
		}
		skin_windowupdate((PSKIN_WINDOW) control->parent);
	}
	return 0;
}

int skin_button_onmousebuttonup(void* self, SDL_Event* e)
{
	PSKIN_BUTTON p = (PSKIN_BUTTON) self;
	if (p)
	{		
		PSKIN_CLIENT control = (PSKIN_CLIENT) p;
		if (p->normal)
		{

		}
		else
		{
			SDL_Rect rect;
			// set default color
			rect.x = rect.y = 0;
			rect.w = control->rect.cx - control->rect.x;
			rect.h = control->rect.cy - control->rect.y;
			if ((p->button_state & BUTTON_HOVER) == BUTTON_HOVER && ptinrect(e->motion.x, e->motion.y, &control->rect))
			{
				SDL_FillRect(control->client_surface, &rect, SDL_MapRGB(control->client_surface->format, 255, 255, 255));
			}
			else
			{
				SDL_FillRect(control->client_surface, &rect, SDL_MapRGB(control->client_surface->format, 128, 128, 128));
			}
		}
		p->button_state = p->button_state & ~(BUTTON_CLICKED);
		skin_windowupdate((PSKIN_WINDOW) control->parent);
		skin_send_event(SKIN_COMMAND, control);
	}
	return 0;
}

/**
	@name 		skin_button_loadbmp
	@date		2013-05-22
	@author		pregene@gmail.com
	@brief		skin_button_loabmp() is to load images of normal, hover and clicked states for button control.
	@return		skin_button_loadbmp() return 1 or 0 if couldn't load an image.
*/
int skin_button_loadbmp(PSKIN_BUTTON p, const char* filename, int option, int x, int y, int cx, int cy)
{
	return 0;
}